<template>
  <div class="forgot-password-container">
    <el-card class="forgot-password-card">
      <template #header>
        <div class="card-header">
          <h2>重置密码</h2>
        </div>
      </template>

      <el-form 
        :model="formState" 
        label-position="top"
        @submit.prevent="onFinish"
      >
        <el-form-item
          label="邮箱"
          prop="loginEmail"
          :rules="[
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
          ]"
        >
          <el-input
            v-model="formState.loginEmail"
            placeholder="请输入注册邮箱"
          >
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item class="form-actions">
          <el-button
            type="primary"
            native-type="submit"
            class="reset-button"
            :loading="loading"
          >
            发送重置链接
          </el-button>
          <router-link to="/login">
            <el-button class="login-button" link>
              返回登录
            </el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Message } from '@element-plus/icons-vue'
import { forgotPassword } from '../api/auth'

const router = useRouter()
const loading = ref(false)

const formState = reactive({
  loginEmail: '',
})

const onFinish = async () => {
  loading.value = true
  try {
    const response = await forgotPassword(formState)
    if (response.success) {
      ElMessage.success('重置链接已发送到您的邮箱')
      router.push('/login')
    }
  } catch (error) {
    ElMessage.error(error.message || '发送重置链接失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.forgot-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}

.forgot-password-card {
  width: 450px;
  :deep(.el-card__header) {
    background-color: #f5f7fa;
    border-bottom: 1px solid #e4e7ed;
  }
}

.form-actions {
  margin-top: 24px;
  display: flex;
  justify-content: space-between;
}

.reset-button {
  width: 100%;
}

.login-button {
  margin-left: auto;
}
</style>